package com.primary.controller;

import com.primary.common.Result;
import com.primary.controller.request.DonorinfoRequest;
import com.primary.entity.Donorinfo;
import com.primary.entity.Users;
import com.primary.service.DonorinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/donorinfo")
public class DonorinfoController {
    @Autowired
    DonorinfoService donorinfoService;

    @RequestMapping(value = "/page", method = {RequestMethod.GET})
    public Result page(DonorinfoRequest donorinfoRequest) {
        return Result.success(donorinfoService.queryByPage(donorinfoRequest));
    }

    @RequestMapping(value = "/getOne/{donors}", method = {RequestMethod.GET})
    public Result getOne(@PathVariable String donors) {
        Donorinfo donorinfo = new Donorinfo();
        donorinfo.setDonors(donors);
        return donorinfoService.queryDonerByDonors(donorinfo) != null ?
                Result.success():Result.error("-1","未查询到您的献血记录");
    }

    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody Donorinfo donorinfo) {
        if (donorinfo.getPhone() != null &&
                donorinfo.getAge() != null &&
                donorinfo.getDonors() != null &&
                donorinfo.getBloodType() != null &&
                donorinfo.getSex() != null) {
            return donorinfoService.insertOne(donorinfo) > 0 ?
                    Result.success() : Result.error("-114", "插入错误");
        } else {
            return Result.error("请输入所有信息");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateOne", method = {RequestMethod.PUT})
    public Result updateOne(@RequestBody Donorinfo donorinfo) {
        if (null != donorinfo.getAge() &&
                null != donorinfo.getPhone()) {
            return donorinfoService.updateOneById(donorinfo) > 0 ?
                    Result.success() : Result.error("更新失败!");
        } else {
            return Result.error("信息不能为空！");
        }
    }

    @RequestMapping(value = "/deleteOne/{id}", method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id) {
        return null == id ? Result.error("ID不能为空") :
                (donorinfoService.deleteById(id) ? Result.success() : Result.error("删除失败"));
    }
}
